/**
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2025，所有权利保留。
 * <p>
 * 项目名： cdx-ai-agent
 * 文件名： MyInvalidAdvisor.java
 * 模块说明：
 * 修改历史：
 * 2025年09月23日 - chendongxu - 创建。
 */
package
        com.cdx.cdxaiagent.advisor;
import reactor.core.publisher.Flux;

import org.springframework.ai.chat.client.advisor.api.AdvisedRequest;
import org.springframework.ai.chat.client.advisor.api.AdvisedResponse;
import org.springframework.ai.chat.client.advisor.api.CallAroundAdvisor;
import org.springframework.ai.chat.client.advisor.api.CallAroundAdvisorChain;
import org.springframework.ai.chat.client.advisor.api.StreamAroundAdvisor;
import org.springframework.ai.chat.client.advisor.api.StreamAroundAdvisorChain;
/**
 * 违禁词校验
 * @author chendongxu
 * @since 1.0
 */
public class MyInvalidAdvisor implements CallAroundAdvisor, StreamAroundAdvisor{
    @Override
    public AdvisedResponse aroundCall(AdvisedRequest advisedRequest, CallAroundAdvisorChain chain) {
        checkVaild(advisedRequest);
        return chain.nextAroundCall(advisedRequest);
    }

    private void checkVaild(AdvisedRequest advisedRequest) {
        if(advisedRequest.userText().contains("cnm")) {
            throw new RuntimeException("有违禁词");
        }
    }

    @Override
    public Flux<AdvisedResponse> aroundStream(AdvisedRequest advisedRequest, StreamAroundAdvisorChain chain) {
        checkVaild(advisedRequest);
        return chain.nextAroundStream(advisedRequest);
    }

    @Override
    public String getName() {
        return "违禁词 Advisor";

    }

    @Override
    public int getOrder() {
        return 0;
    }
}
